package com.campus.client.view;

import com.campus.client.controller.StudentInfoController;
import com.campus.client.net.ClientConnector;
import com.campus.common.vo.AcademicRecord;
import com.campus.common.vo.Student;
import com.campus.common.vo.User;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Modality;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.IOException;

public class StudentInfoFrame {

    private StudentInfoController controller;
    private Student student = new Student();
    private User user = new User();

    // UI组件
    private Label studentIdLabel, recordIdLabel, nameLabel, ageLabel, sexLabel,
            gradeLabel, classNameLabel, majorLabel, stateLabel, enrollDateLabel;
    private Label contactInfoLabel;
    private Button modifyButton;
    private Button backButton;

    public StudentInfoFrame() throws IOException {
    }

    public void setStudent(String studentId, String name, AcademicRecord academicRecord){
        student.setStudentId(studentId);
        student.setStudentName(name);
        student.setAcademicRecord(academicRecord);
    }

    public void setUser(String username, String password,String role){
        user.setId(username);
        user.setPassword(password);
        user.setRole(role);
    }

    public User getUser(){return user;}
    public String getContactInfo(){return contactInfoLabel.getText();}

    public void start(Stage primaryStage) throws IOException, ClassNotFoundException {
        // 设置主布局
        BorderPane root = new BorderPane();
        root.setPadding(new Insets(10));
        root.getStyleClass().add("main-container");

        // 顶部标题
        Label titleLabel = new Label("我的信息");
        titleLabel.setFont(Font.font("System", FontWeight.BOLD, 24));
        titleLabel.getStyleClass().add("header-title");

        HBox titleBox = new HBox(titleLabel);
        titleBox.setAlignment(Pos.CENTER);
        titleBox.setPadding(new Insets(10, 0, 20, 0));
        root.setTop(titleBox);

        // 内容区域 - 学生信息
        GridPane infoGrid = new GridPane();
        infoGrid.setHgap(15);
        infoGrid.setVgap(15);
        infoGrid.setPadding(new Insets(20));
        infoGrid.getStyleClass().add("info-grid");

        // 按钮区域
        HBox buttonBox = new HBox(15);
        buttonBox.setAlignment(Pos.CENTER);
        buttonBox.setPadding(new Insets(20, 0, 10, 0));

        backButton = new Button("返回");
        backButton.getStyleClass().add("secondary-button");
        backButton.setOnAction(e ->{
            back();
        });

        modifyButton = new Button("修改");
        modifyButton.getStyleClass().add("primary-button");

        buttonBox.getChildren().addAll(backButton, modifyButton);

        ClientConnector connector =new ClientConnector("172.20.10.2", 8888);
        controller=new StudentInfoController(connector);

        student=connector.getStudentById(user.getId()).getStudent();

        // 添加信息标签和值
        int row = 0;
        infoGrid.add(createHeaderLabel("学生ID:"), 0, row);
        studentIdLabel = createValueLabel(student.getStudentId());
        infoGrid.add(studentIdLabel, 1, row++);

        infoGrid.add(createHeaderLabel("学籍号:"), 0, row);
        recordIdLabel = createValueLabel(student.getAcademicRecord().getRecordId());
        infoGrid.add(recordIdLabel, 1, row++);

        infoGrid.add(createHeaderLabel("姓名:"), 0, row);
        nameLabel = createValueLabel(student.getStudentName());
        infoGrid.add(nameLabel, 1, row++);

        infoGrid.add(createHeaderLabel("年龄:"), 0, row);
        ageLabel = createValueLabel(String.valueOf(student.getAcademicRecord().getAge()));
        infoGrid.add(ageLabel, 1, row++);

        infoGrid.add(createHeaderLabel("性别:"), 0, row);
        sexLabel = createValueLabel(student.getAcademicRecord().getSex());
        infoGrid.add(sexLabel, 1, row++);

        infoGrid.add(createHeaderLabel("年级:"), 0, row);
        gradeLabel = createValueLabel(student.getAcademicRecord().getGrade());
        infoGrid.add(gradeLabel, 1, row++);

        infoGrid.add(createHeaderLabel("班级:"), 0, row);
        classNameLabel = createValueLabel(student.getAcademicRecord().getClassName());
        infoGrid.add(classNameLabel, 1, row++);

        infoGrid.add(createHeaderLabel("专业:"), 0, row);
        majorLabel = createValueLabel(student.getAcademicRecord().getMajor());
        infoGrid.add(majorLabel, 1, row++);

        infoGrid.add(createHeaderLabel("状态:"), 0, row);
        stateLabel = createValueLabel(student.getAcademicRecord().getStatus());
        infoGrid.add(stateLabel, 1, row++);

        infoGrid.add(createHeaderLabel("入学时间:"), 0, row);
        enrollDateLabel = createValueLabel(student.getAcademicRecord().getEnrollDate());
        infoGrid.add(enrollDateLabel, 1, row++);

        infoGrid.add(createHeaderLabel("联系方式:"), 0, row);
        contactInfoLabel = createValueLabel(student.getAcademicRecord().getContactInfo());
        contactInfoLabel.getStyleClass().add("contact-label");
        infoGrid.add(contactInfoLabel, 1, row++);

        VBox contentBox = new VBox(20, infoGrid, buttonBox);
        contentBox.setAlignment(Pos.CENTER);
        contentBox.setPadding(new Insets(10));

        // 将内容放入滚动面板
        ScrollPane scrollPane = new ScrollPane(contentBox);
        scrollPane.setFitToWidth(true);
        scrollPane.setFitToHeight(true);
        scrollPane.getStyleClass().add("scroll-pane");

        root.setCenter(scrollPane);

        // 设置按钮事件
        setupButtonEvents();

        // 创建场景并显示
        Scene scene = new Scene(root, 800, 600);
        scene.getStylesheets().add(getClass().getResource("/css/infoStyle.css").toExternalForm());
        primaryStage.setTitle("我的信息");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void setupButtonEvents() {
        // 修改按钮事件 - 弹出编辑对话框
        modifyButton.setOnAction(e -> {
            showEditDialog();
        });
    }

    private void showEditDialog() {
        // 创建对话框
        Stage dialog = new Stage();
        dialog.initModality(Modality.APPLICATION_MODAL);
        dialog.setTitle("修改联系方式");

        // 创建对话框内容
        VBox dialogVBox = new VBox(15);
        dialogVBox.setPadding(new Insets(20));
        dialogVBox.setAlignment(Pos.CENTER);

        // 创建联系方式编辑区域
        HBox contactBox = new HBox(10);
        contactBox.setAlignment(Pos.CENTER_LEFT);

        Label contactLabel = new Label("联系方式:");
        contactLabel.setFont(Font.font("System", FontWeight.BOLD, 14));

        TextField contactField = new TextField(contactInfoLabel.getText());
        contactField.setPrefWidth(200);

        contactBox.getChildren().addAll(contactLabel, contactField);

        // 创建按钮区域
        HBox buttonBox = new HBox(15);
        buttonBox.setAlignment(Pos.CENTER);

        Button confirmButton = new Button("确认");
        confirmButton.getStyleClass().add("primary-button");
        confirmButton.setOnAction(e -> {
            try {
                if(controller.sendRequest(user.getId(),contactField.getText(),student.getAcademicRecord().getContactInfo())){
                    JOptionPane.showMessageDialog(null,"请求发送成功！请等待审核！","请求成功",JOptionPane.INFORMATION_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null,"请求发送失败！你可能已经发过相同的请求！","请求失败",JOptionPane.ERROR_MESSAGE);
                }
                dialog.close();
            } catch (ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        Button cancelButton = new Button("取消");
        cancelButton.getStyleClass().add("secondary-button");
        cancelButton.setOnAction(e -> dialog.close());

        buttonBox.getChildren().addAll(confirmButton, cancelButton);

        dialogVBox.getChildren().addAll(contactBox, buttonBox);

        // 创建场景并显示对话框
        Scene dialogScene = new Scene(dialogVBox, 350, 150);
        dialogScene.getStylesheets().add(getClass().getResource("/css/infoStyle.css").toExternalForm());
        dialog.setScene(dialogScene);
        dialog.showAndWait();
    }

    private Label createHeaderLabel(String text) {
        Label label = new Label(text);
        label.setFont(Font.font("System", FontWeight.BOLD, 14));
        label.getStyleClass().add("info-header");
        return label;
    }

    private Label createValueLabel(String value) {
        Label label = new Label(value);
        label.setFont(Font.font("System", 14));
        label.getStyleClass().add("info-value");
        return label;
    }


    private void back() {
        // 获取当前窗口的Stage
        Stage currentStage = (Stage) backButton.getScene().getWindow();
        currentStage.close();

        // 这里可以添加返回主界面的逻辑
        // 例如：打开管理员主界面
        try {
            StudentMainFrame studentMainFrame = new StudentMainFrame();
            studentMainFrame.setUser(user.getId(), user.getPassword(), user.getRole());
            studentMainFrame.show(new Stage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}